﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeberTimeTracker.Models;

namespace WeberTimeTracker.Controllers
{ 
    public class ProjectController : Controller
    {
        private TimeTrackerContext db = new TimeTrackerContext();

        //
        // GET: /Project/

        public ViewResult Index()
        {
            var projects = GetProjects();
            return View(projects);
        }

        //GET: /Project/Disable

        public ViewResult Disable(int ID)
        {
            
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                Project projects = db.Projects.Find(ID);
                return View(projects);
            }
        }

        //POST: /Project/Disable
        [HttpPost]
        public ViewResult Disable(int ID, Project projects)
        {
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                var updateToInactive = (from i in data.Projects where i.Active == true && i.Project_ID == ID select i).ToList(); //Project projects projects.Project_
                foreach (Project p in updateToInactive)
                {
                    p.Active = false;
                }
                data.SaveChanges();
                return View("Index", GetProjects());
            }          
        }

        //
        // GET: /Project/Details/5

        public ViewResult Details(int id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            ViewBag.Active = true;
            ViewBag.Client_ID = new SelectList(db.Clients, "Client_ID", "Name");
            ViewBag.Rate_ID = new SelectList(db.Rates, "Rate_ID", "Rate_ID");
            return View();
        } 

        //
        // POST: /Project/Create
        [HttpPost]
        public ActionResult Create(Project project)
        {
            if (ModelState.IsValid)
            {
                project.Active = true;
                db.Projects.Add(project);
                db.SaveChanges();
                var projects = GetProjects();
                return RedirectToAction("Index", projects);  
            }

            ViewBag.Client_ID = new SelectList(db.Clients, "Client_ID", "Name", project.Client_ID);
            ViewBag.Rate_ID = new SelectList(db.Rates, "Rate_ID", "Rate_ID", project.Rate_ID);
            return View(project);
        }
        
        //
        // GET: /Project/Edit/5
 
        public ActionResult Edit(int id)
        {
            Project project = db.Projects.Find(id);
            ViewBag.Client_ID = new SelectList(db.Clients, "Client_ID", "Name", project.Client_ID);
            ViewBag.Rate_ID = new SelectList(db.Rates, "Rate_ID", "Rate_ID", project.Rate_ID);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
               // return RedirectToAction("Index");
            }
            ViewBag.Client_ID = new SelectList(db.Clients, "Client_ID", "Name", project.Client_ID);
            ViewBag.Rate_ID = new SelectList(db.Rates, "Rate_ID", "Rate_ID", project.Rate_ID);
            return View(project);
        }

        //
        // GET: /Project/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    Project project = db.Projects.Find(id);
        //    return View(project);
        //}

        ////
        //// POST: /Project/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{            
        //    Project project = db.Projects.Find(id);
        //    db.Projects.Remove(project);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        private static IEnumerable<Project> GetProjects()
        {
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                var ProjList = data.Database.SqlQuery<Project>("[dbo].[Get_Project]").ToList();
                return ProjList;
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}